############################## bkmr单变量暴漏  #############################
##### 科研工具4 贝叶斯回归-单一变量暴露
library(ggplot2)
library(bkmr)
library(readr)
setwd('父级路径')
demo <- read_csv("source.csv")

# demoS5 <-demo[,c('Age','Sex','race','edu','FPL','activity','smoke',
#                  'drink','Hypertension','CKD','urinecreatinine','BMI','MECPP',
#                  'MnBP','MEHHP','MEOHP','MiBP','MiNP','MCOP','MCPP','MEP','MBzP',
#                  'Hyperuricemiaint','LBDSUASI')]

tryCatch({
  alldataform
},error = function(e){
  print("获取数据出错")
})

#demoS5 <- demoS5[c(1:200),]
y <- demoS5$Hyperuricemiaint
# Z <- demoS5[,c('MECPP','MnBP','MEHHP','MEOHP','MiBP','MiNP',
#                'MCOP','MCPP','MEP','MBzP')]
tryCatch({
  xdatarep
},error = function(e){
  print("获取数据出错")
})



# X <- demoS5[,c('Age','Sex','race','edu','FPL','activity','smoke',
#                'drink','Hypertension','CKD','urinecreatinine','BMI')]

tryCatch({
  logparseparam
},error = function(e){
  print("log 转换出错提示")
})

set.seed(2012)
fitkm <- kmbayes(y, Z = Z, X = NULL, iter = 1000, verbose = FALSE, varsel = TRUE,family = 'binomial',est.h = TRUE)
pred.resp.univar <- PredictorResponseUnivar(fit = fitkm)
Cairo::CairoTIFF(file="PredictorResponseUnivar.tiff", width=8, height=8,units="in",dpi=150)
ggplot(pred.resp.univar, aes(z, est, ymin = est - 1.96*se, ymax = est + 1.96*se)) + 
  geom_smooth(stat = "identity") + 
  facet_wrap(~ variable) +
  ylab("h(expos)") +
  xlab("Phthalate metabolites") +
  xlim(-2,6)
dev.off()
